package com.example.other.controller;

import cn.hutool.core.date.DateUtil;
import com.example.other.common.Constant;
import com.example.other.entity.ClassInfo;
import com.example.other.entity.Columns;
import com.example.other.entity.ReqDtoParams;
import com.example.other.utils.VelocityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.*;

@Slf4j
@RestController
public class DemoController {


    @Autowired
    private VelocityUtils velocityUtils;


    @GetMapping("/demo")
    public List<String> getDemo(){
        velocityUtils.initVelocity();

        ReqDtoParams reqDtoParams = new ReqDtoParams();


        reqDtoParams.setPackageName("com.example.other.entity");
        reqDtoParams.setClassDesc("类描述");
        reqDtoParams.setAuthor("yufan.wang");
        reqDtoParams.setDatetime(DateUtil.now());
        reqDtoParams.setClassName("className");
        reqDtoParams.setReturnClassName("returnClassName");

        Columns columns = new Columns();
        columns.setColumnComment("测试字段A");
        columns.setJavaType("Integer");
        columns.setJavaField("aaa");

        Columns columns1 = new Columns();
        columns1.setColumnComment("测试字段B");
        columns1.setJavaType("String");
        columns1.setJavaField("bbb");

        Columns columns2 = new Columns();
        columns2.setColumnComment("测试字段C");
        columns2.setJavaType("String");
        columns2.setJavaField("ccc");

        reqDtoParams.setColumns(Arrays.asList(columns, columns1, columns2));


        VelocityContext context = velocityUtils.prepareContext(reqDtoParams);
        List<String> templateList = velocityUtils.getTemplateList();
        templateList.forEach(template->{
            // 渲染模板
            StringWriter sw = new StringWriter();

            Template tpl = Velocity.getTemplate(template, Constant.UTF8);
            tpl.merge(context, sw);

            ClassInfo classInfo = new ClassInfo();
            BeanUtils.copyProperties(reqDtoParams,classInfo);

            try {
                String genPath = velocityUtils.getGenPath(classInfo, template);
                FileUtils.writeStringToFile(new File(genPath), sw.toString(), Constant.UTF8);
            } catch (IOException e) {
                throw new RuntimeException("渲染模板失败："+classInfo.getClassName());
            }
        });

        return velocityUtils.getTemplateList();
    }
}

